#include "ddBitmap.h"

ddBitmap::ddBitmap(void)
{
}

ddBitmap::~ddBitmap(void)
{
}

LPDIRECTDRAWSURFACE7 ddBitmap::loadBitmapFile(LPCWSTR filename, ddUtilities ddUtil, bool colorkey)
{
	HDC srcDC;
	HDC destDC;
	HBITMAP hBitmap;
	LPDIRECTDRAWSURFACE7 lpdds;

	HANDLE tmp = LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	hBitmap = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	srcDC = CreateCompatibleDC(NULL);
	SelectObject(srcDC, hBitmap);

	GetObject(hBitmap, sizeof(ddBitmap), this);

	lpdds = ddUtil.ddCreateSurface(this->bmWidth, this->bmHeight, DDSCAPS_VIDEOMEMORY, colorkey);
	ddUtil.ddFillSurface(lpdds, 0);
	lpdds->GetDC(&destDC);
	BitBlt(destDC, 0, 0, this->bmWidth, this->bmHeight, srcDC, 0, 0, SRCCOPY);

	lpdds->ReleaseDC(destDC);
	DeleteDC(srcDC);
	return lpdds;
}


int ddBitmap::ddDrawSprite(LPDIRECTDRAWSURFACE7 source, 
						   LPDIRECTDRAWSURFACE7 destination, 
						   int x, int y,
						   int width, int height, 
						   int row, int column)
{
	RECT dest, src;

	dest.left = x;
	dest.top = y;
	dest.right = dest.left + width - 1;
	dest.bottom = dest.top + height - 1;

	src.left = width * column;
	src.top = height * row;
	src.right = src.left + width - 1;
	src.bottom = src.top + height - 1;

	
	if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT | DDBLT_KEYSRC, NULL)))
		return 0;

	return 1;
}

int ddBitmap::ddDrawFullBackground(player player1,
								   LPDIRECTDRAWSURFACE7 source,
								   LPDIRECTDRAWSURFACE7 destination)
{
	RECT dest, src;

	int imgStartPos = player1.y < 0 ? this->bmHeight + player1.y : player1.y;
	int screenHeight = MIN(this->bmHeight - imgStartPos, SCREEN_HEIGHT);
	int heightStart = 0;

	while (screenHeight <= SCREEN_HEIGHT)
	{	
		int imageWidth = this->bmWidth;
		int startPos = player1.x;
		int screenWidth = SCREEN_WIDTH;

		if (imageWidth <= screenWidth)
		{
			screenWidth = 0;

			while (startPos < 0)
			{
				startPos += imageWidth;
			}
			while (startPos >= imageWidth)
			{
				startPos -= imageWidth;
			}

			while (screenWidth <= SCREEN_WIDTH)
			{
				dest.left = screenWidth;
				dest.top = heightStart;
				dest.right = dest.left + MAX(0, MIN(imageWidth - startPos, SCREEN_WIDTH - screenWidth));
				dest.bottom = screenHeight;

				src.left = startPos;
				src.top = imgStartPos;
				src.right = src.left + MIN(imageWidth - startPos, SCREEN_WIDTH - screenWidth);
				src.bottom = src.top + (screenHeight - heightStart);

				if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
					break;

				screenWidth += MIN(imageWidth - startPos, SCREEN_WIDTH - screenWidth);
				startPos = 0;
			}
		}
		else
		{
			if (player1.x < 0)
			{
				dest.left = 0;
				dest.top = heightStart;
				dest.right = dest.left + MIN(SCREEN_WIDTH, -1*player1.x);
				dest.bottom = screenHeight;

				src.left = imageWidth + player1.x;
				src.top = imgStartPos;
				src.right = src.left + MIN(SCREEN_WIDTH, -1*player1.x);
				src.bottom = src.top + (screenHeight - heightStart);

				if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
					return(0);

				if (dest.right < SCREEN_WIDTH)
				{
					dest.left = (-1 * player1.x);
					dest.top = heightStart;
					dest.right = dest.left + ( SCREEN_WIDTH + player1.x );
					dest.bottom = screenHeight;

					src.left = 0;
					src.top = imgStartPos;
					src.right = src.left + ( SCREEN_WIDTH + player1.x );
					src.bottom = src.top + (screenHeight - heightStart);

					if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
						return(0);
				}
			}
			else if ((player1.x + SCREEN_WIDTH) > imageWidth)
			{
				dest.left = 0;
				dest.top = heightStart;
				dest.right = dest.left + (this->bmWidth - player1.x);
				dest.bottom = screenHeight;

				src.left = player1.x;
				src.top = imgStartPos;
				src.right = src.left + (this->bmWidth - player1.x);
				src.bottom = src.top + (screenHeight - heightStart);

				if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
					return(0);

				dest.left = (this->bmWidth - player1.x);
				dest.top = heightStart;
				dest.right = SCREEN_WIDTH;
				dest.bottom = screenHeight;

				src.left = 0;
				src.top = imgStartPos;
				src.right = SCREEN_WIDTH - this->bmWidth + player1.x;
				src.bottom = src.top + (screenHeight - heightStart);

				if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
					return(0);
			}
			else
			{
				dest.left = 0;
				dest.top = heightStart;
				dest.right = dest.left + SCREEN_WIDTH;
				dest.bottom = screenHeight;

				src.left = player1.x;
				src.top = imgStartPos;
				src.right = src.left + SCREEN_WIDTH;
				src.bottom = src.top + (screenHeight - heightStart);
				if (FAILED(destination->Blt(&dest, source, &src, DDBLT_WAIT, NULL)))
					return(0);
			}
		}

		heightStart += MIN(this->bmHeight - imgStartPos, SCREEN_HEIGHT);
		imgStartPos = 0;
		screenHeight += MIN(this->bmHeight - imgStartPos, SCREEN_HEIGHT - heightStart);
	}
	return(1);
}